package org.dbstar.scheduler.control.db.conf;

import java.lang.reflect.InvocationTargetException;

import org.dbstar.scheduler.beans.ParameterDefine;
import org.dbstar.scheduler.beans.ParameterDefineType;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.NewBeanInstanceStrategy;
import net.sf.json.util.PropertyFilter;


/**
 * 用于转换ParameterDefine的JsonConfig
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-4-26
 */
public class ParameterDefineJsonConfig extends JsonConfig {
	public ParameterDefineJsonConfig() {
		super();
		setRootClass(ParameterDefine.class);
		setNewBeanInstanceStrategy(new NewBeanInstanceStrategy() {
			@Override
			public Object newInstance(@SuppressWarnings("rawtypes") Class target, JSONObject source)
					throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException,
					InvocationTargetException {
				if (ParameterDefine.class == target) {
					ParameterDefine pd = new ParameterDefine(source.getString("name"),
							ParameterDefineType.valueOf(source.getString("type")), source.optString("value", null));
					return pd;
				} else {
					return null;
				}
			}
		});
		setJsonPropertyFilter(new PropertyFilter() {
			@Override
			public boolean apply(Object source, String name, Object value) {
				return value == null;
			}
		});
		setJavaPropertyFilter(new PropertyFilter() {
			@Override
			public boolean apply(Object source, String name, Object value) {
				return true;
			}
		});
	}
}
